<template>
  <div>
    <el-card class="box-card" style="margin:30px">
      <div slot="header" class="clearfix">
        <el-button type="primary" size="mini" @click="add">新增</el-button>
      </div>
      <el-table
        :data="list"
        border
        style="width: 100%"
      >
        <el-table-column
          fixed
          type="index"
          label="序号"
          width="50"
          align="center"
        />
        <el-table-column
          prop="name"
          label="品牌名称"
          width="120"
          align="center"
        />
        <el-table-column label="展示状态" width="180" align="center">
          <template slot-scope="scope">
            <el-switch v-model="scope.row.showStatus" :active-value="1" :inactive-value="0" aria-setsize="small" inactive-text="是否展示" @change="switchStatus(scope.row)" />
          </template>
        </el-table-column>
        <el-table-column label="logo" width="180" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.logo" style="width:100px;height:100px">
          </template>
        </el-table-column>
        <el-table-column label="专区大图" width="180" align="center">
          <template slot-scope="scope">
            <img :src="scope.row.bigPic" style="width:100px;height:100px">
          </template>
        </el-table-column>
        <el-table-column
          prop="brandStory"
          label="品牌故事"
          width="180"
          align="center"
        />
        <el-table-column
          prop="createTime"
          label="创建时间"
          width="300"
          align="center"
        />
        <el-table-column
          prop="sort"
          label="排序"
          width="100"
          align="center"
        />
        <el-table-column
          prop="productCommentCount"
          label="产品评论数量"
          width="100"
          align="center"
        />
        <el-table-column
          prop="productCount"
          label="产品数量"
          width="120"
          align="center"
        />
        <el-table-column label="是否品牌制造商" width="180" align="center">
          <template slot-scope="scope">
            <div>{{ scope.row.factoryStatus === 1 ?'是':'否' }}</div>
          </template>
        </el-table-column>
        <el-table-column
          fixed="right"
          label="操作"
          width="100"
        >
          <template slot-scope="scope">
            <el-button type="text" size="small" @click="edit(scope.row)">编辑</el-button>
            <el-button type="text" size="small" style="color:#f00" @click="del(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-pagination
        :current-page="page.currentpage"
        :page-sizes="page.pagesizes"
        :page-size="page.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </el-card>

    <detail ref="details" @success="savesuccess" />
  </div>
</template>
<script>
import { findBrandByPage, switchShowStatus, delBrand } from '@/api/product/brand'
import detail from './components/detail'
export default {
  components: {
    detail
  },
  data() {
    return {
      list: [],
      page: {
        currentpage: 1,
        pagesizes: [10, 20, 30],
        pagesize: 10,
        total: 0
      }
    }
  },
  computed: {
  },
  created() {
    this.getList()
  },
  methods: {
    getList() {
      findBrandByPage(this.page.currentpage, this.page.pagesize).then(res => {
        console.log(res)
        this.list = res.data.rows
        this.page.total = res.data.total
      })
    },
    switchStatus(val) {
      var currentStatus = val.status === 0 ? 1 : 0
      switchShowStatus({
        id: val.id,
        status: val.showStatus
      }).then(res => {
        if (res.success) {
          this.$message.success('修改成功')
        } else {
          this.$message.error('修改失败')
          val.status = currentStatus
        }
      })
    },
    handleSizeChange(val) {
      this.page.pagesize = val
      this.getList()
    },
    handleCurrentChange(val) {
      this.page.currentpage = val
      this.getList()
    },
    add() {
      this.$refs.details.open()
    },
    edit(val) {
      this.$refs.details.open(val)
    },
    del(val) {
      this.$confirm('此操作将永久删除, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delBrand(val.id).then(res => {
          if (res.success) {
            this.$message.success('删除成功')
            this.getList()
          } else {
            this.$message.error('删除失败')
          }
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    savesuccess() {
      this.getList()
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
